package com.xm.zcf.action.user;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xm.zcf.domain.cart.CarItem;
import com.xm.zcf.domain.cart.Cart;
import com.xm.zcf.service.product.ProductService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/4/5.
 */
@Controller
@Scope("prototype")
public class CartAction extends ActionSupport {
    private Integer pid;
    private Integer count;

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    @Resource
    private ProductService productService;
    public String addCart(){
        CarItem carItem=new CarItem();
        carItem.setProduct(productService.findByPid(pid));
        carItem.setCount(count);
        Cart cart=getCart();
        cart.addCart(carItem);
        return "cart";
    }
    public String clearCart(){
        return "cart";
    }
    public String removeCart(){
        Cart cart=getCart();
        cart.removeItem(pid);
        return "cart";
    }
    public String findCart(){
        return "cart";
    }

    public Cart getCart() {
        Cart cart= (Cart)ActionContext.getContext().getSession().get("cart");
        if(cart==null){
            cart=new Cart();
            ActionContext.getContext().getSession().put("cart",cart);
        }
        return cart;
    }
}
